package com.yx.cloud.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import com.yx.cloud.R;
import com.yx.cloud.bean.MapManagerItemBean;
import com.yx.cloud.map.YXMapView;
import com.yx.cloud.map.layer.YXCleanAreaLayer;
import com.yx.cloud.map.layer.YXCustomPathLayer;
import com.yx.cloud.map.layer.YXForbiddenAreaLayer;
import com.yx.cloud.map.layer.YXForbiddenLineLayer;
import com.yx.cloud.map.layer.YXForbiddenMopAreaLayer;
import com.yx.cloud.map.layer.YXPowerLayer;
import com.yx.cloud.map.layer.YXRoomTagLayer;
import com.yx.cloud.map.layer.YXSweeperLayer;
import com.yx.cloud.utils.RomUtils;
import java.util.List;

public class MultiMapListAdapter extends  RecyclerView.Adapter implements View.OnClickListener{
    private static final String TAG = "MultiMapListAdapter";

    private Context context;
    private List<MapManagerItemBean> list;
    private MultiMapListAdapter.InnerItemOnclickListener listener;
    FragmentManager fmm;
    int choosePos = 0;

    public MultiMapListAdapter(Context context, List<MapManagerItemBean> list, FragmentManager fmm) {
        this.context = context;
        this.list = list;
        this.fmm = fmm;
    }
    public void  updateData(List<MapManagerItemBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public void  selectItem(int position){
        choosePos = position;
        listener.itemClick(position);
        notifyDataSetChanged();
    }

    public int getSelectedItem(){
        return choosePos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View viewLocalMap = LayoutInflater.from(context).inflate(R.layout.map_manager_multi_map,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(viewLocalMap);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //获取数据得到类型
        MapManagerItemBean data = list.get(position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.tvMapName.setText(data.getName());
        myViewHolder.rootView.setOnClickListener(this);
        myViewHolder.rootView.setTag(position);
        //更新地图
        myViewHolder.yxMapView.setBackgroundColor(Color.WHITE);
        myViewHolder.yxMapView.initMap(data.getMap());
        myViewHolder.yxMapView.setTouchable(false);
        myViewHolder.yxMapView.setRoomTagMode(false,false,false,false,false,false, false);
        myViewHolder.yxMapView.setLayerVisible(false,
                YXPowerLayer.class, YXSweeperLayer.class, YXRoomTagLayer.class,
                YXForbiddenMopAreaLayer.class, YXForbiddenAreaLayer.class, YXForbiddenLineLayer.class, YXCleanAreaLayer.class,
                YXCustomPathLayer.class);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) myViewHolder.llMapName.getLayoutParams();

        if(choosePos == position){
            myViewHolder.tvMapName.setTextColor(context.getResources().getColor(R.color.button_color2));
            myViewHolder.rootView.setBackgroundResource(R.drawable.selected_item_background);
            params.setMargins(3,0,3,3);
        }else {
            myViewHolder.tvMapName.setTextColor(context.getResources().getColor(R.color.textview_color2));
            myViewHolder.rootView.setBackgroundResource(R.drawable.shape_background_white2);
            params.setMargins(0,0,0,0);
        }
        myViewHolder.llMapName.setLayoutParams(params);
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        listener.itemClick((int)v.getTag());
        choosePos = (int)v.getTag();
        notifyDataSetChanged();
    }

    public interface InnerItemOnclickListener {
        void itemClick(int position);
    }

    public void setOnInnerItemOnClickListener(MultiMapListAdapter.InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        LinearLayout rootView;
        LinearLayout llMapName;
        TextView tvMapName;
        YXMapView yxMapView;
        public MyViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.ll_multi_item);
            llMapName = itemView.findViewById(R.id.ll_multi_map_name);
            tvMapName = itemView.findViewById(R.id.tv_multi_map_name);
            yxMapView = itemView.findViewById(R.id.fl_multi_map);
        }
    }
}
